package com.shawn.springboot.integeration.validation.common.exception;

import com.shawn.springboot.integeration.validation.common.model.GenericResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    private Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    // 系统异常
    @ExceptionHandler(Exception.class)
    public GenericResponse handleException(Exception e) {
        log.error(e.getMessage(), e);
        return GenericResponse.error("服务器错误，请联系管理员");
    }

    @ExceptionHandler(BindException.class)
    public GenericResponse validatedBindException(BindException e) {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return GenericResponse.error(message);
    }
}